<?php
/**
 * 春哥团队（www.cgtblog.com） - 春哥多端多商户万能DIY商城系统
 * Copyright © 2024 春哥技术团队 保留所有权利
 * =========================================================

 * ----------------------------------------------
 * 您只能在商业授权范围内使用，不可二次转售、分发、分享、传播
 * 任何企业和个人不得对代码以任何目的任何形式的再发布
 * =========================================================
 */

namespace app\common;
use think\facade\Db;
use think\facade\Log;

class Wifiprint
{
    //打印小票 aid 模块 订单号
    public static function print($aid,$type,$orderid,$autoprint=1,$machineType=-1,$bid=-1){
        $order = db($type.'_order')->where('id',$orderid)->find();
        if($bid!=='' && $bid>-1){
            $bid = $bid;
        }else{
            $bid = $order['bid']??0;
        }
        $where = [];
        $where[] = ['aid', '=', $aid];
        $where[] = ['status', '=', 1];
        if($autoprint == 1) {
            $where[] = ['autoprint', '=', $autoprint];
        }
        if($machineType>-1){
            $where[] = ['machine_type', '=', $machineType];
        }
        if(getcustom('wifiprint_bind_user') && uid>0){
            //该管理员绑定的打印机
            $print_ids = Db::name('wifiprint_user')->where('aid',$aid)->where('bid',$bid)->where('uid',uid)->column('print_id');
            if(empty($print_ids)) $print_ids = [];
            $print_ids[] = -1;//没有绑定打印机
            $where[] = ['id','in',$print_ids];
        }
        $machinelist = db('wifiprint_set')->where($where)->where('bid',$bid)->select()->toArray(); //打印机列表
        if(!$machinelist) return ['status'=>0,'msg'=>'没有配置小票打印机'];
        //if($type == 'shop' && $order['bid']!=0) return [];
        $order['formdata'] = \app\model\Freight::getformdata($order['id'],$type.'_order');

        if(getcustom('sys_print_set')){
            //记录今天打印了几次
            $nowtime = strtotime(date("Y-m-d",time()));
            $printdaynum        = 'print_day_ordernum'.$aid.$bid.$nowtime;
            $print_day_ordernum = '';
            if($order['printdaynum']){
                if($type == 'shop' || $type == 'cashier' || $type == 'maidan' || $type == 'scoreshop'){
                    $print_day_ordernum = $order['printdaynum'];
                }
            }else{
                $print_day_ordernum = cache($printdaynum);
                if(!$print_day_ordernum || empty($print_day_ordernum)){
                    cache($printdaynum,1);
                    $print_day_ordernum = 1;
                }
            }
            //打印状态次数
            $print_status = 0;
        }

        $printnum = 0;
        foreach($machinelist as $machine){
            if($order['freight_type']==0 && $machine['print_ps']==0){ //配送订单
                continue;
            }
            if($type == 'maidan' &&  $machine['print_maidan']==0){ //买单订单
                continue;
            }
            if($type == 'cashier' &&  $machine['print_cashdesk']==0){ //收银台订单
                continue;
            }
            if($order['freight_type']==1 || $order['freight_type']==5){ //自提订单
                if($machine['print_zt_type']==0) continue;
                if($machine['print_zt_type']==2){ //指定门店
                    $mdids = explode(',',$machine['print_zt_mdid']);
                    if(!in_array($order['mdid'],$mdids)) continue;
                }
            }
            $num = 1;
            if(getcustom('sys_print_set')){
                //打印次数
                $num =  $machine['print_num']?$machine['print_num']:1;
            }
            for($i=0;$i<$num;$i++){
                if($machine['type']==0){
                    $tmpltype = 0;
                    if(getcustom('sys_print_set')){
                        //易联云小票自定义（暂没使用，使用的是底部自定义）
                        $tmpltype =  $machine['tmpltype']?$machine['tmpltype']:0;
                    }
                    if(!$tmpltype){
                        $content = '';
                        $content = "<MS>1,".$machine['voice']."</MS>";
                        if(getcustom('sys_print_set')){
                            if($print_day_ordernum && $machine['day_ordernum']){
                                $content .=  '<FB><center>#'.$print_day_ordernum."</center></FB>\r\r";
                            }
                        }
                        $content .= "<center>@@2 ** ".$machine['title']." **</center>\r\r";
                        if($type=='maidan'){
                            $content .= "订单标题：".$order['title']."\r";
                        }
                        $content .= "订单编号：".$order['ordernum']."\r";
                        if($type=='cashier' || $type=='maidan'){
                            //收银台无收货信息
                        }else{
                            $content .= "配送方式：".$order['freight_text']."\r";
                            if($order['freight_time']){
                                $content .= "配送时间：<FS>".$order['freight_time']."</FS>\r";
                            }
                            $content .= "收货人:<FS>".$order['linkman']."</FS>\r";
                            $content .= "联系电话:<FS>".$order['tel']."</FS>\r";
                            $content .= "收货地址:<FS>".$order['area']." ".$order['address']."</FS>\r";
                        }
                        if($order['paytime']){
                            $content .= "付款时间：".date('Y-m-d H:i:s',$order['paytime'])."\r";
                        }
                        $content .= "付款方式：".$order['paytype']."\r\r";
                        $content .="--------------------------------\r";
                        if($type=='maidan'){ //买单没有商品信息
                            //买单没有商品信息
                        }elseif ($type=='cycle'){
                            $content .= "<table>";
                            $content .= "<tr><td>商品名称</td><td>数量</td><td>期数</td><td>总价</td></tr>";
                        }elseif($type=='shop' && isset($order['product_type']) && $order['product_type']==2){
                            //称重订单
                            $content .= "<table>";
                            $content .= "<tr><td>商品名称</td><td>单价</td><td>重量</td><td>总价</td></tr>";
                        }else{
                            $content .= "<table>";
                            $content .= "<tr><td>商品名称</td><td>数量</td><td>总价</td></tr>";
                        }
                        if($type == 'shop'){
                            $ordergoods = db($type.'_order_goods')->where('orderid',$order['id'])->select()->toArray();
                            foreach($ordergoods as $item){
                                if($type=='shop' && isset($order['product_type']) && $order['product_type']==2){
                                    $content .= "<tr><td><FB>".$item['name'].'('.$item['ggname'].')'."</FB></td><td>".$item['real_sell_price']."</td><td>".$item['real_total_weight']."</td><td>".$item['real_totalprice']."</td></tr>";
                                }else{
                                    $content .= "<tr><td><FB>".$item['name'].'('.$item['ggname'].')'."</FB></td><td>".$item['num']."</td><td>".$item['totalprice']."</td></tr>";
                                }
                                if(getcustom('product_glass')){
                                    //打印追加：视力档案输出
                                    $grrowArr = \app\model\ShopOrder::getGlassRecordRow($item);
                                    if($grrowArr){
                                        $row1 = str_replace('</p>','',str_replace('<p>','',$grrowArr['row1']));
                                        $row2 = str_replace('</p>','',str_replace('<p>','',$grrowArr['row2']));
                                        $row3 = str_replace('</p>','',str_replace('<p>','',$grrowArr['row3']));
                                        $content .= "<tr><td colspan='3'>".$row1."</td></tr>";
                                        $content .= "<tr><td colspan='3'>".$row2."</td></tr>";
                                        $content .= "<tr><td colspan='3'>".$row3."</td></tr>";
                                    }
                                }
                            }
                        }elseif($type == 'cycle'){
                            $content .= "<tr><td><FB>".$order['proname'].'('.$order['ggname'].')'."</FB></td><td>".$order['num']."</td><td>".$order['qsnum']."</td><td>".$order['totalprice']."</td></tr>";
                        }elseif($type=='collage'){
                            $content .= "<tr><td><FB>".$order['proname'].'('.$order['ggname'].')'."</FB></td><td>".$order['num']."</td><td>".$order['totalprice']."</td></tr>";
                        }elseif($type=='lucky_collage'){
                            $content .= "<tr><td><FB>".$order['proname'].'('.$order['ggname'].')'."</FB></td><td>".$order['num']."</td><td>".$order['totalprice']."</td></tr>";
                        }elseif($type=='seckill'){
                            $content .= "<tr><td><FB>".$order['proname'].'('.$order['ggname'].')'."</FB></td><td>".$order['num']."</td><td>".$order['totalprice']."</td></tr>";
                        }elseif($type=='kanjia'){
                            $content .= "<tr><td>".$order['proname']."</td><td>1</td><td>".$order['totalprice']."</td></tr>";
                        }elseif($type=='tuangou'){
                            $content .= "<tr><td>".$order['proname']."</td><td>".$order['num']."</td><td>".$order['totalprice']."</td></tr>";
                        }elseif($type=='scoreshop'){

                            $ordergoods = db('scoreshop_order_goods')->where('orderid',$order['id'])->select()->toArray();
                            foreach($ordergoods as $item){
                                if($item['totalmoney'] > 0 && $item['totalscore'] > 0){
                                    $price = $item['totalmoney']."元+".$item['totalscore'].t('积分');
                                }elseif($item['totalmoney'] > 0){
                                    $price = $item['totalmoney']."元";
                                }else{
                                    $price = $item['totalscore'].t('积分');
                                }

                                $content .= "<tr><td><FB>".$item['name']."</FB></td><td>".$item['num']." </td><td>".$price."</td></tr>";
                            }
                        }elseif($type=='cashier'){
                            $ordergoods = db('cashier_order_goods')->where('orderid',$order['id'])->select()->toArray();
                            foreach($ordergoods as $item){
                                $content .= "<tr><td><FB>".$item['proname'].'('.$item['ggname'].')'."</FB></td><td>".$item['num']."</td><td>".$item['totalprice']."</td></tr>";
                            }
                            $order['message'] = $order['remark'];
                        }
                        if($type=='maidan'){ //买单没有商品信息
                            //买单没有商品信息
                        }else{
                            $content .= "</table>";
                            $content .= "\r";
                        }
                        if($type=='shop'){
                            $order['message'] = \app\model\ShopOrder::checkOrderMessage($orderid,$order);
                            if($order['message']){
                                $content .= "备注：<FS>".$order['message']."</FS>\r";
                            }else{
                                //$content .= "备注：无\r";
                            }

                        }
                       
                        if($type=='scoreshop'){
                            $content .="--------------------------------\r";
                            $content .= "<LR>实付金额：,".$price."</LR>";
                        }elseif($type=='cashier'){
                            $content .="--------------------------------\r";
                            $content .= "<LR>订单总价：,".dd_money_format($order['pre_totalprice'])."</LR>";
                            if($order['scoredk_money']>0){
                                $content .= "<LR>积分抵扣：,".$order['scoredk_money']."</LR>";
                            }
                            if($order['coupon_money']>0) {
                                $content .= "<LR>优惠券抵扣：," . dd_money_format($order['coupon_money']) . "</LR>";
                            }
                            if($order['leveldk_money']>0) {
                                $content .= "<LR>会员折扣：," . dd_money_format($order['leveldk_money']) . "</LR>";
                            }
                            if($order['moling_money']>0) {
                                $content .= "<LR>抹零金额：," . dd_money_format($order['moling_money']) . "</LR>";
                            }
                            $content .= "<LR>实付金额：,".dd_money_format($order['totalprice'])."</LR>";
                        }elseif($type=='maidan'){
                            $content .= "<LR>订单总价：,".dd_money_format($order['money'])."</LR>";
                            if($order['scoredk_money']>0){
                                $content .= "<LR>积分抵扣：,".$order['scoredk']."</LR>";
                            }
                            if($order['leveldk_money']>0) {
                                $content .= "<LR>会员折扣：," . dd_money_format($order['disprice']) . "</LR>";
                            }
                            if($order['decmoney']>0) {
                                $content .= "<LR>余额抵扣：," . dd_money_format($order['decmoney'] ). "</LR>";
                            }
                            if($order['couponmoney']>0) {
                                $content .= "<LR>优惠券抵扣：," . dd_money_format($order['couponmoney']) . "</LR>";
                            }
                            $content .= "<LR>实付金额：,".dd_money_format($order['paymoney'])."</LR>";
                        }else{
                            $content .="--------------------------------\r";
                            $content .= "<LR>实付金额：,".dd_money_format($order['totalprice'])."</LR>";
                        }
                        if($order['formdata']) {
                            foreach ($order['formdata'] as $formdata) {
                                if($formdata[2] != 'upload') {
                                    if($formdata[0] == '备注') {
                                        $content .= $formdata[0]."：<FS>".$formdata[1]."</FS>\r\r";
                                    } else {
                                        $content .= $formdata[0]."：<FS>".$formdata[1]."</FS>\r";
                                    }
                                }
                            }
                        }
                        if(getcustom('sys_print_set')){
                            //易联云小票底部自定义
                            $boot_custom =  $machine['boot_custom']?$machine['boot_custom']:0;
                            if($boot_custom){
                                $deal_content = \app\custom\WifiprintCustom::deal_content($type,$order,$machine,$print_day_ordernum,2);
                                if($deal_content && $deal_content['status'] == 1){
                                    $content .= "\r\r".$deal_content['data'];
                                }
                            }
                        }
                        $content .= "\r\r";
                        $rs = self::yilianyun_print($machine['client_id'],$machine['client_secret'],$machine['access_token'],$machine['machine_code'],$machine['msign'],$content);
                        $printnum++;
                    }else{
                        if(getcustom('sys_print_set')){
                            //易联云小票自定义
                            $deal_content = \app\custom\WifiprintCustom::deal_content($type,$order,$machine,$print_day_ordernum,1);
                            if($deal_content && $deal_content['status'] == 1){
                                $content = $deal_content['data'];

                                //易联云小票底部自定义
                                $boot_custom =  $machine['boot_custom']?$machine['boot_custom']:0;
                                if($boot_custom){
                                    $deal_content = \app\custom\WifiprintCustom::deal_content($type,$order,$machine,$print_day_ordernum,2);
                                    if($deal_content && $deal_content['status'] == 1){
                                        $content .= "\r\r".$deal_content['data'];
                                    }
                                }

                                $content .= "\r\r";
                                $rs = self::yilianyun_print($machine['client_id'],$machine['client_secret'],$machine['access_token'],$machine['machine_code'],$machine['msign'],$content);
                                $printnum++;
                            }else{
                                continue;
                            }
                        }
                    }
                }
                elseif($machine['type']==1){
                    if($machine['machine_type']==1){ //标签打印机
                        if($type == 'shop'){
                            $ordergoods = db($type.'_order_goods')->where('orderid',$order['id'])->select()->toArray();
                            $count = count($ordergoods);
                            $showType = 'shop';
                            //称重商品打印
                            if(getcustom('product_weight') && $order['product_type']==2){
                                $showType = 'weightShop';
                                if($order['customer_id']>0){
                                    $customer = Db::name('sh_customer')->where('id',$order['customer_id'])->find();
                                    $pcustomer = [];
                                    if($customer['pid']>0){
                                        $pcustomer = Db::name('sh_customer')->where('id',$customer['pid'])->find();
                                    }
                                }elseif($order['mid']>0){
                                    $member = Db::name('member')->where('aid',aid)->where('id',$order['mid'])->find();
                                }
                                $count = count($ordergoods);
                                foreach ($ordergoods as $k=>$item){
                                   // [只打印已发货的有效订单]
                                    if(in_array($item['status'],[0,1,4])){
                                        continue;
                                    }
                                    $content = '';
                                    $content.= '<TEXT x="5" y="20" font="12" w="1" h="1" r="0">#'.$order['ordernum'].'('.($k+1).'/'.$count.')</TEXT>';
                                    $height = 20;
                                    $stepLen = 30;
                                    if($order['customer_id']>0){
                                        if($pcustomer){
                                            $height = $height+$stepLen;
                                            //$content.= '<B x="9" y="'.$height.'">客户：'.$pcustomer['name'].'</B>';
                                            $content.= '<TEXT x="5" y="'.$height.'" font="12" w="2" h="2" r="0">'.$pcustomer['name'].'</TEXT>';
                                        }
                                        if($customer){
                                            $height = $height+2*$stepLen;
                                            $customerName = $customer['name'].($customer['number']?'('.$customer['number'].')':'');
                                            $content.= '<TEXT x="5" y="'.$height.'" font="12" w="2" h="2" r="0">'.$customerName.'</TEXT>';
                                        }
                                    }else if($order['mid']>0){
                                        $height = $height+$stepLen;
                                        $content.= '<TEXT x="5" y="'.$height.'" font="12" w="2" h="2" r="0">客户：'.$member['nickname'].'</TEXT>';
                                    }

                                    $height = $height + 2*$stepLen;
                                    $content .= '<TEXT x="5" y="'.$height.'" font="12" w="2" h="2" r="0">'.$item['name'].$item['ggname'].'</TEXT>';
                                    $height = $height + $stepLen*2;
                                    /*$content .= '<TEXT x="9" y="'.$height.'" font="12" w="1" h="1" r="0">'.$item['real_sell_price'].'元/斤,'.round($item['real_total_weight']/500,2).'斤,￥'.$item['real_totalprice'].'</TEXT>';*/
                                    $content .= '<TEXT x="5" y="'.$height.'" font="12" w="1" h="1" r="0">购买数量：'.$item['num'].'  ，  分拣重量：'.round($item['real_total_weight']/500,2).'斤</TEXT>';
                                    $height = $height + $stepLen;
                                    $content .= '<TEXT x="5" y="'.$height.'" font="12" w="2" h="2" r="0">'.$item['remark'].'</TEXT>';
                                    $rs = self::feie_print($machine['client_id'],$machine['client_secret'],$machine['machine_code'],$machine['msign'],$content,1);
                                    $printnum++;
                                }

                            }
                            if($showType=='shop'){
                                foreach($ordergoods as $k=>$item){
                                    $content = '';
                                    $content .= '<TEXT x="9" y="5" font="12" w="1" h="1" r="0">#'.$order['ordernum'].'   '.($k+1).'/'.$count.'</TEXT>';
                                    $content .= '<TEXT x="9" y="40" font="12" w="1" h="1" r="0">'.$item['name'].'</TEXT>';
                                    $content .= '<TEXT x="9" y="70" font="12" w="1" h="1" r="0">'.$item['ggname'] .' × '.$item['num'].'  共'.$item['totalprice'].'元</TEXT>';
                                    $content .= '<TEXT x="9" y="100" font="12" w="1" h="1" r="0">配送方式:'.$order['freight_text'].'</TEXT>';
                                    $content .= '<TEXT x="9" y="130" font="12" w="1" h="1" r="0">收货人:'.$order['linkman'] .'('.$order['tel'].')</TEXT>';
                                    $content .= '<TEXT x="9" y="155" font="12" w="1" h="1" r="0">'.$order['area'].'</TEXT>';
                                    $content .= '<TEXT x="9" y="180" font="12" w="1" h="1" r="0">'.$order['address'].'</TEXT>';
                                    $content .= '<TEXT x="9" y="210" font="12" w="1" h="1" r="0">实付金额：'.$order['totalprice'].'</TEXT>';

                                    if($machine['print_qrcode'] == 1){
                                        if($item['barcode']){
                                            $content .= '<QR x="270"  y="210"  e="L"  w="5">'.$item['barcode'].'</QR>';
                                        }elseif($item['procode']){
                                            $content .= '<QR x="270"  y="210"  e="L"  w="5">'.$item['procode'].'</QR>';
                                        }
                                    }
                                    if(getcustom('print_label_barcode')){
                                        if($machine['print_barcode'] == 1){
                                            if($machine['print_qrcode']){
                                                if($item['barcode']){
                                                    $content .= '<BC128 x="9" y="320" s="1" r="0" n="2" w="4">'.$item['barcode'].'</BC128>';
                                                }elseif($item['procode']){
                                                    $content .= '<BC128 x="9" y="320" s="1" r="0" n="2" w="4">'.$item['procode'].'</BC128>';
                                                }
                                            }else{
                                                if($item['barcode']){
                                                    $content .= '<BC128 x="9" y="240" s="1" r="0" n="2" w="4">'.$item['barcode'].'</BC128>';
                                                }elseif($item['procode']){
                                                    $content .= '<BC128 x="9" y="240" s="1" r="0" n="2" w="4">'.$item['procode'].'</BC128>';
                                                }
                                            }
                                        }
                                    }
                                    $rs = self::feie_print($machine['client_id'],$machine['client_secret'],$machine['machine_code'],$machine['msign'],$content,1);
                                    $printnum++;
                                }
                            }
                        }elseif($type=='collage'){
                            $content = '';
                            $content .= '<TEXT x="9" y="5" font="12" w="1" h="1" r="0">#'.$order['ordernum'].'   '.($k+1).'/'.$count.'</TEXT>';
                            $content .= '<TEXT x="9" y="40" font="12" w="1" h="1" r="0">'.$order['proname'].'</TEXT>';
                            $content .= '<TEXT x="9" y="70" font="12" w="1" h="1" r="0">    × '.$order['num'].'  共'.$order['totalprice'].'元</TEXT>';
                            $content .= '<TEXT x="9" y="100" font="12" w="1" h="1" r="0">配送方式:'.$order['freight_text'].'</TEXT>';
                            $content .= '<TEXT x="9" y="130" font="12" w="1" h="1" r="0">收货人:'.$order['linkman'] .'('.$order['tel'].')</TEXT>';
                            $content .= '<TEXT x="9" y="155" font="12" w="1" h="1" r="0">'.$order['area'].'</TEXT>';
                            $content .= '<TEXT x="9" y="180" font="12" w="1" h="1" r="0">'.$order['address'].'</TEXT>';
                            $content .= '<TEXT x="9" y="210" font="12" w="1" h="1" r="0">实付金额：'.$order['totalprice'].'</TEXT>';
                            $rs = self::feie_print($machine['client_id'],$machine['client_secret'],$machine['machine_code'],$machine['msign'],$content,1);
                            $printnum++;
                        }elseif($type=='lucky_collage'){
                            $content = '';
                            $content .= '<TEXT x="9" y="5" font="12" w="1" h="1" r="0">#'.$order['ordernum'].'   '.($k+1).'/'.$count.'</TEXT>';
                            $content .= '<TEXT x="9" y="40" font="12" w="1" h="1" r="0">'.$order['proname'].'</TEXT>';
                            $content .= '<TEXT x="9" y="70" font="12" w="1" h="1" r="0">    × '.$order['num'].'  共'.$order['totalprice'].'元</TEXT>';
                            $content .= '<TEXT x="9" y="100" font="12" w="1" h="1" r="0">配送方式:'.$order['freight_text'].'</TEXT>';
                            $content .= '<TEXT x="9" y="130" font="12" w="1" h="1" r="0">收货人:'.$order['linkman'] .'('.$order['tel'].')</TEXT>';
                            $content .= '<TEXT x="9" y="155" font="12" w="1" h="1" r="0">'.$order['area'].'</TEXT>';
                            $content .= '<TEXT x="9" y="180" font="12" w="1" h="1" r="0">'.$order['address'].'</TEXT>';
                            $content .= '<TEXT x="9" y="210" font="12" w="1" h="1" r="0">实付金额：'.$order['totalprice'].'</TEXT>';
                            $rs = self::feie_print($machine['client_id'],$machine['client_secret'],$machine['machine_code'],$machine['msign'],$content,1);
                            $printnum++;
                        }elseif($type=='seckill'){
                            $content = '';
                            $content .= '<TEXT x="9" y="5" font="12" w="1" h="1" r="0">#'.$order['ordernum'].'   '.($k+1).'/'.$count.'</TEXT>';
                            $content .= '<TEXT x="9" y="40" font="12" w="1" h="1" r="0">'.$order['proname'].'</TEXT>';
                            $content .= '<TEXT x="9" y="70" font="12" w="1" h="1" r="0">    × '.$order['num'].'  共'.$order['totalprice'].'元</TEXT>';
                            $content .= '<TEXT x="9" y="100" font="12" w="1" h="1" r="0">配送方式:'.$order['freight_text'].'</TEXT>';
                            $content .= '<TEXT x="9" y="130" font="12" w="1" h="1" r="0">收货人:'.$order['linkman'] .'('.$order['tel'].')</TEXT>';
                            $content .= '<TEXT x="9" y="155" font="12" w="1" h="1" r="0">'.$order['area'].'</TEXT>';
                            $content .= '<TEXT x="9" y="180" font="12" w="1" h="1" r="0">'.$order['address'].'</TEXT>';
                            $content .= '<TEXT x="9" y="210" font="12" w="1" h="1" r="0">实付金额：'.$order['totalprice'].'</TEXT>';
                            $rs = self::feie_print($machine['client_id'],$machine['client_secret'],$machine['machine_code'],$machine['msign'],$content,1);
                            $printnum++;
                        }elseif($type=='kanjia'){
                            $content = '';
                            $content .= '<TEXT x="9" y="5" font="12" w="1" h="1" r="0">#'.$order['ordernum'].'   '.($k+1).'/'.$count.'</TEXT>';
                            $content .= '<TEXT x="9" y="40" font="12" w="1" h="1" r="0">'.$order['proname'].'</TEXT>';
                            $content .= '<TEXT x="9" y="70" font="12" w="1" h="1" r="0">    × 1  共'.$order['totalprice'].'元</TEXT>';
                            $content .= '<TEXT x="9" y="100" font="12" w="1" h="1" r="0">配送方式:'.$order['freight_text'].'</TEXT>';
                            $content .= '<TEXT x="9" y="130" font="12" w="1" h="1" r="0">收货人:'.$order['linkman'] .'('.$order['tel'].')</TEXT>';
                            $content .= '<TEXT x="9" y="155" font="12" w="1" h="1" r="0">'.$order['area'].'</TEXT>';
                            $content .= '<TEXT x="9" y="180" font="12" w="1" h="1" r="0">'.$order['address'].'</TEXT>';
                            $content .= '<TEXT x="9" y="210" font="12" w="1" h="1" r="0">实付金额：'.$order['totalprice'].'</TEXT>';
                            $rs = self::feie_print($machine['client_id'],$machine['client_secret'],$machine['machine_code'],$machine['msign'],$content,1);
                            $printnum++;
                        }elseif($type=='tuangou'){
                            $content = '';
                            $content .= '<TEXT x="9" y="5" font="12" w="1" h="1" r="0">#'.$order['ordernum'].'   '.($k+1).'/'.$count.'</TEXT>';
                            $content .= '<TEXT x="9" y="40" font="12" w="1" h="1" r="0">'.$order['proname'].'</TEXT>';
                            $content .= '<TEXT x="9" y="70" font="12" w="1" h="1" r="0">    × '.$order['num'].'  共'.$order['totalprice'].'元</TEXT>';
                            $content .= '<TEXT x="9" y="100" font="12" w="1" h="1" r="0">配送方式:'.$order['freight_text'].'</TEXT>';
                            $content .= '<TEXT x="9" y="130" font="12" w="1" h="1" r="0">收货人:'.$order['linkman'] .'('.$order['tel'].')</TEXT>';
                            $content .= '<TEXT x="9" y="155" font="12" w="1" h="1" r="0">'.$order['area'].'</TEXT>';
                            $content .= '<TEXT x="9" y="180" font="12" w="1" h="1" r="0">'.$order['address'].'</TEXT>';
                            $content .= '<TEXT x="9" y="210" font="12" w="1" h="1" r="0">实付金额：'.$order['totalprice'].'</TEXT>';
                            $rs = self::feie_print($machine['client_id'],$machine['client_secret'],$machine['machine_code'],$machine['msign'],$content,1);
                            $printnum++;
                        }elseif($type=='scoreshop'){
                            $content = '';
                            $content .= '<TEXT x="9" y="5" font="12" w="1" h="1" r="0">#'.$order['ordernum'].'   1/1</TEXT>';
                            $content .= '<TEXT x="9" y="40" font="12" w="1" h="1" r="0">'.$order['proname'].'</TEXT>';

                            if($order['totalprice'] > 0 && $order['score_price'] > 0){
                                $price = $order['totalprice']."元+".$order['score_price'].t('积分');
                            }elseif($order['totalprice'] > 0){
                                $price = $order['totalprice']."元";
                            }else{
                                $price = $order['score_price'].t('积分');
                            }
                            $content .= '<TEXT x="9" y="70" font="12" w="1" h="1" r="0"> ×'.$order['num'].' 共'.$price.'</TEXT>';
                            $content .= '<TEXT x="9" y="100" font="12" w="1" h="1" r="0">配送方式:'.$order['freight_text'].'</TEXT>';
                            $content .= '<TEXT x="9" y="130" font="12" w="1" h="1" r="0">收货人:'.$order['linkman'] .'('.$order['tel'].')</TEXT>';
                            $content .= '<TEXT x="9" y="155" font="12" w="1" h="1" r="0">'.$order['area'].'</TEXT>';
                            $content .= '<TEXT x="9" y="180" font="12" w="1" h="1" r="0">'.$order['address'].'</TEXT>';
                            $content .= '<TEXT x="9" y="210" font="12" w="1" h="1" r="0">实付金额：'.$order['totalprice'].'</TEXT>';
                            $rs = self::feie_print($machine['client_id'],$machine['client_secret'],$machine['machine_code'],$machine['msign'],$content,1);
                            $printnum++;
                        }elseif($type == 'cashier'){
                            $ordergoods = db($type.'_order_goods')->where('orderid',$order['id'])->select()->toArray();
                            $count = count($ordergoods);
                            foreach($ordergoods as $k=>$item){
                                $content = '';
                                $content .= '<TEXT x="9" y="5" font="12" w="1" h="1" r="0">#'.$order['ordernum'].'   '.($k+1).'/'.$count.'</TEXT>';
                                $content .= '<TEXT x="9" y="40" font="12" w="1" h="1" r="0">'.$item['name'].'</TEXT>';
                                $content .= '<TEXT x="9" y="70" font="12" w="1" h="1" r="0">'.$item['ggname'] .' × '.$item['num'].'  共'.$item['totalprice'].'元</TEXT>';
                                $content .= '<TEXT x="9" y="100" font="12" w="1" h="1" r="0">优惠券抵扣:'.$order['coupon_money'].'</TEXT>';
                                $content .= '<TEXT x="9" y="130" font="12" w="1" h="1" r="0">会员折扣:'.$order['leveldk_money'].'</TEXT>';
                                $content .= '<TEXT x="9" y="155" font="12" w="1" h="1" r="0">积分抵扣:'.$order['scoredk_money'].'</TEXT>';
                                $content .= '<TEXT x="9" y="180" font="12" w="1" h="1" r="0">抹零金额:'.$order['moling_money'].'</TEXT>';
                                $content .= '<TEXT x="9" y="210" font="12" w="1" h="1" r="0">订单总价：'.$order['pre_totalprice'].'</TEXT>';
                                $content .= '<TEXT x="9" y="240" font="12" w="1" h="1" r="0">实付金额：'.$order['totalprice'].'</TEXT>';
                                $rs = self::feie_print($machine['client_id'],$machine['client_secret'],$machine['machine_code'],$machine['msign'],$content,1);
                                $printnum++;
                            }
                        }elseif($type == 'maidan'){
                            $content = '';
                            $content .= '<TEXT x="9" y="5" font="12" w="1" h="1" r="0">#'.$order['ordernum'].'</TEXT>';
                            $content .= '<TEXT x="9" y="40" font="12" w="1" h="1" r="0">'.$order['title'].'</TEXT>';
                            $content .= '<TEXT x="9" y="100" font="12" w="1" h="1" r="0">优惠券抵扣:'.$order['couponmoney'].'</TEXT>';
                            $content .= '<TEXT x="9" y="130" font="12" w="1" h="1" r="0">会员折扣:'.$order['disprice'].'</TEXT>';
                            $content .= '<TEXT x="9" y="155" font="12" w="1" h="1" r="0">积分抵扣:'.$order['scoredk'].'</TEXT>';
                            $content .= '<TEXT x="9" y="210" font="12" w="1" h="1" r="0">订单总价：'.$order['money'].'</TEXT>';
                            $content .= '<TEXT x="9" y="240" font="12" w="1" h="1" r="0">实付金额：'.$order['paymoney'].'</TEXT>';
                            $rs = self::feie_print($machine['client_id'],$machine['client_secret'],$machine['machine_code'],$machine['msign'],$content,1);
                            $printnum++;
                        }
                    }else{
                        if($machine['tmpltype']==1){
                            if($type == 'shop'){
                                $ordergoods = db('shop_order_goods')->where('orderid',$order['id'])->select()->toArray();
                                $order['num'] = db('shop_order_goods')->where('orderid',$order['id'])->sum('num');
                            }elseif($type=='scoreshop'){
                                $ordergoods = db('scoreshop_order_goods')->where('orderid',$order['id'])->select()->toArray();
                                $order['num'] = db('scoreshop_order_goods')->where('orderid',$order['id'])->sum('num');
                            }else{
                                $ordergoods = [['name'=>$order['proname'],'ggname'=>$order['ggname'],'num'=>$order['num'],'totalprice'=>$order['product_price']]];
                            }
                            if($type=='scoreshop'){
                                if($order['totalprice'] > 0 && $order['score_price'] > 0){
                                    $order['totalprice'] = $order['totalprice']."元 + ".$order['score_price'].t('积分');
                                }elseif($order['totalprice'] > 0){
                                    $order['totalprice'] = $order['totalprice']."元";
                                }else{
                                    $order['totalprice'] = $order['score_price'].t('积分');
                                }
                            }elseif($type == 'cashier'){
                                $ordergoods = db('cashier_order_goods')->where('orderid',$order['id'])->select()->toArray();
                                $order['num'] = db('cashier_order_goods')->where('orderid',$order['id'])->sum('num');
                            }
                            $message = '';
                            if($order['formdata']) {
                                foreach ($order['formdata'] as $formdata) {
                                    if($formdata[2] != 'upload') {
                                        if($formdata[0] == '备注') {
                                            $message = $formdata[1];
                                        }
                                    }
                                }
                            }

                            $tmplcontent = $machine['tmplcontent'];
                            if(strpos($tmplcontent,'<PLOOP>')!==false){
                                $tmplcontentArr = explode('<PLOOP>',$tmplcontent);
                                $tmplcontent1 = $tmplcontentArr[0];
                                $tmplcontentArr = explode('</PLOOP>',$tmplcontentArr[1]);
                                $tmplcontent2 = $tmplcontentArr[0];
                                $tmplcontent3 = $tmplcontentArr[1];
                            }else{
                                $tmplcontent1 = $tmplcontent;
                                $tmplcontent2 = '';
                                $tmplcontent3 = '';
                            }
                            if($type=='maidan'){
                                $textReplaceArr = [
                                    '[订单号]'=>$order['ordernum'],
                                    '[付款时间]'=>date('Y-m-d H:i:s',$order['paytime']),
                                    '[付款方式]'=>$order['paytype'],
                                    '[价格]'=>$order['money'],
                                    '[实付金额]'=>$order['paymoney'],
                                    '[备注]'=>$message,
                                ];
                            }elseif ($type=='shop' && isset($order['product_type']) && $order['product_type']==2){
                                //称重商品
                                if(getcustom('product_weight')){
                                    $customer = Db::name('sh_customer')->where('id',$order['customer_id'])->find();
                                    $customer_name = $customer?$customer['name']:'';
                                    if($customer['pid']){
                                        $pcustomer_name =  Db::name('sh_customer')->where('id',$customer['pid'])->value('name');
                                        if($pcustomer_name) $customer_name = $pcustomer_name.' '.$customer_name;
                                    }
                                    $textReplaceArr = [
                                        '[订单号]'=>$order['ordernum'],
                                        '[客户]'=>$customer_name,
                                        '[配送方式]'=>$order['freight_text'],
                                        '[收货人]'=>$order['linkman'],
                                        '[联系电话]'=>$order['tel'],
                                        '[收货地址]'=>$order['address'],
                                        '[付款方式]'=>$order['paytype'],
                                        '[商品名称]'=>$order['title'],
                                        '[实付金额]'=>$order['totalprice'],
                                        '[备注]'=>$message,
                                    ];
                                }
                            }else{
                                $textReplaceArr = [
                                    '[订单号]'=>$order['ordernum'],
                                    '[配送方式]'=>$order['freight_text'],
                                    '[配送时间]'=>$order['freight_time'],
                                    '[收货人]'=>$order['linkman'],
                                    '[联系电话]'=>$order['tel'],
                                    '[收货地址]'=>$order['area']." ".$order['address'],
                                    '[付款时间]'=>date('Y-m-d H:i:s',$order['paytime']),
                                    '[付款方式]'=>$order['paytype'],
                                    '[商品名称]'=>$order['title'],
                                    '[数量]'=>$order['num'],
                                    '[价格]'=>$order['product_price'],
                                    '[实付金额]'=>$order['totalprice'],
                                    '[备注]'=>$message,
                                ];
                            }
                            $tmplcontent1 = str_replace(array_keys($textReplaceArr),array_values($textReplaceArr),$tmplcontent1);
                            if($tmplcontent3){
                                $tmplcontent3 = str_replace(array_keys($textReplaceArr),array_values($textReplaceArr),$tmplcontent3);
                            }
                            if($tmplcontent2){
                                $tmplcontent2Arr = [];
                                foreach($ordergoods as $item){
                                    if($type=='scoreshop'){
                                        if($item['totalscore'] > 0 && $item['totalmoney'] > 0){
                                            $item['totalprice'] = $item['totalmoney']."元 + ".$item['totalscore'].t('积分');
                                        }elseif($item['totalmoney'] > 0){
                                            $item['totalprice'] = $item['totalmoney']."元";
                                        }else{
                                            $item['totalprice'] = $item['totalscore'].t('积分');
                                        }
                                    }else{
                                        $item['totalprice'] = ''.$item['totalprice'];
                                    }
                                    $textReplaceArr2 = [
                                        '[订单号]'=>$order['ordernum'],
                                        '[配送方式]'=>$order['freight_text'],
                                        '[配送时间]'=>$order['freight_time'],
                                        '[收货人]'=>$order['linkman'],
                                        '[联系电话]'=>$order['tel'],
                                        '[收货地址]'=>$order['area']." ".$order['address'],
                                        '[付款时间]'=>date('Y-m-d H:i:s',$order['paytime']),
                                        '[付款方式]'=>$order['paytype'],
                                        '[商品名称]'=>$item['name'],
                                        '[规格]'=>$item['ggname'],
                                        '[数量]'=>$item['num'],
                                        '[价格]'=>$item['totalprice'],
                                        '[实付金额]'=>$order['totalprice'],
                                        '[备注]'=>$message,
                                    ];
                                    $tmplcontent2Arr[] = str_replace(array_keys($textReplaceArr2),array_values($textReplaceArr2),$tmplcontent2);
                                }
                                $tmplcontent2 = implode('',$tmplcontent2Arr);
                            }
                            $content =  '';
                            if(getcustom('sys_print_set')){
                                if($print_day_ordernum && $machine['day_ordernum']){
                                    $content .=  '<CB>#'.$print_day_ordernum."</CB><BR><BR>";
                                }
                            }
                            $content .= $tmplcontent1.$tmplcontent2.$tmplcontent3;

                            if(getcustom('sys_print_set')){
                                //飞蛾小票底部自定义
                                $boot_custom =  $machine['boot_custom']?$machine['boot_custom']:0;
                                if($boot_custom){
                                    $boot_custom_content = $machine['boot_custom_content'];
                                    if($boot_custom_content){
                                        $content .= "<BR><BR>".$boot_custom_content;
                                    }
                                }
                            }

                            $content = str_replace(["\r","\n"],'',$content);
                        }else{
                            $content = '';
                            if(getcustom('sys_print_set')){
                                if($print_day_ordernum && $machine['day_ordernum']){
                                    $content .=  '<CB>#'.$print_day_ordernum."</CB><BR><BR>";
                                }
                            }
                            $content .= "<CB>** ".$machine['title']." **</CB><BR><BR>";
                            
                            if($type=='maidan'){
                                $content .= "订单标题：".$order['title']."<BR>";
                            }
                            $content .= "订单编号：".$order['ordernum']."<BR>";
                            if($type=='cashier' || $type=='maidan'){
                                //收银台无收货信息
                            }else{
                                $content .= "配送方式：".$order['freight_text']."<BR>";
                                if($order['freight_time']){
                                    $content .= "配送时间：<B>".$order['freight_time']."</B><BR>";
                                }
                                $content .= "收货人:<B>".$order['linkman']."</B><BR>";
                                $content .= "联系电话:<B>".$order['tel']."</B><BR>";
                                $content .= "收货地址:<B>".$order['area']." ".$order['address']."</B><BR>";
                            }
                            $content .= "付款时间：".date('Y-m-d H:i:s',$order['paytime'])."<BR>";
                            $content .= "付款方式：".$order['paytype']."<BR><BR>";
                            if($type=='maidan'){
                                //买单没有商品信息
                            }elseif($type=='cycle'){
                                $content .= "商品名称     数量     期数     总价<BR>";
                            }else{
                                $content .= "商品名称     数量     总价<BR>";
                            }
                            if($type == 'shop'){
                                $ordergoods = db($type.'_order_goods')->where('orderid',$order['id'])->select()->toArray();
                                foreach($ordergoods as $item){
                                    $content .= "<BOLD>".$item['name']."(".$item['ggname'].")</BOLD>   ".$item['num']."  ".$item['totalprice']."<BR>";
                                }
                            }elseif($type=='cycle'){
                                $content .= "<BOLD>".$order['proname']."(".$order['ggname'].")</BOLD>   ".$order['num']."   ".$order['qsnum']."  ".$order['totalprice']."<BR>";
                            }elseif($type=='collage'){
                                $content .= "<BOLD>".$order['proname']."(".$order['ggname'].")</BOLD>   ".$order['num']."  ".$order['totalprice']."<BR>";
                            }elseif($type=='lucky_collage'){
                                $content .= "<BOLD>".$order['proname']."(".$order['ggname'].")</BOLD>   ".$order['num']."  ".$order['totalprice']."<BR>";
                            }elseif($type=='seckill'){
                                $content .= "<BOLD>".$order['proname']."(".$order['ggname'].")</BOLD>   ".$order['num']."  ".$order['totalprice']."<BR>";
                            }elseif($type=='kanjia'){
                                $content .= "".$order['proname']."  1  ".$order['totalprice']."<BR>";
                            }elseif($type=='tuangou'){
                                $content .= "".$order['proname']."  ".$order['num']."  ".$order['totalprice']."<BR>";
                            }elseif($type=='scoreshop'){
                                $ordergoods = db('scoreshop_order_goods')->where('orderid',$order['id'])->select()->toArray();
                                foreach($ordergoods as $item){
                                    if($item['totalmoney'] > 0 && $item['totalscore'] > 0){
                                        $price = $item['totalmoney']."元+".$item['totalscore'].t('积分');
                                    }elseif($item['totalmoney'] > 0){
                                        $price = $item['totalmoney']."元";
                                    }else{
                                        $price = $item['totalscore'].t('积分');
                                    }
                                    $content .= "<BOLD>".$item['name']."</BOLD>   ".$item['num']."  ".$price."<BR>";
                                }
                            }elseif($type == 'cashier'){
                                $ordergoods = db($type.'_order_goods')->where('orderid',$order['id'])->select()->toArray();
                                foreach($ordergoods as $item){
                                    $content .= "<BOLD>".$item['proname']."(".$item['ggname'].")</BOLD>   ".$item['num']."  ".$item['totalprice']."<BR>";
                                }
                                $order['message'] = $order['remark'];
                            }
                            $content .= "<BR>";
                            if($order['message']){
                                $content .= "备注：<B>".$order['message']."</B><BR><BR>";
                            }else{
                                //						$content .= "备注：无<BR>";
                            }
                            if($type=='scoreshop'){
                                $content .= "<RIGHT>实付金额：".$price."</RIGHT>";
                            }elseif($type=='cashier'){
                                if($order['scoredk_money']>0){
                                    $content .= "<RIGHT>积分抵扣：".$order['scoredk_money']."</RIGHT>";
                                }
                                if($order['coupon_money']>0) {
                                    $content .= "<RIGHT>优惠券抵扣：" . $order['coupon_money'] . "</RIGHT>";
                                }
                                if($order['leveldk_money']>0) {
                                    $content .= "<RIGHT>会员折扣：" . $order['leveldk_money'] . "</RIGHT>";
                                }
                                if($order['moling_money']>0) {
                                    $content .= "<RIGHT>抹零金额：" . $order['moling_money'] . "</RIGHT>";
                                }
                                $content .= "<RIGHT>订单总价：".$order['pre_totalprice']."</RIGHT>";
                                $content .= "<RIGHT>实付金额：".$order['totalprice']."</RIGHT>";
                            }elseif($type=='maidan'){

                                if($order['scoredk_money']>0){
                                    $content .= "<RIGHT>积分抵扣：".$order['scoredk']."</RIGHT>";
                                }
                                if($order['leveldk_money']>0) {
                                    $content .= "<RIGHT>会员折扣：" . $order['disprice'] . "</RIGHT>";
                                }
                                if($order['decmoney']>0) {
                                    $content .= "<RIGHT>余额抵扣：" . $order['decmoney'] . "</RIGHT>";
                                }
                                if($order['couponmoney']>0) {
                                    $content .= "<RIGHT>优惠券抵扣：" . $order['couponmoney'] . "</RIGHT>";
                                }
                                $content .= "<RIGHT>订单总价：".$order['money']."</RIGHT>";
                                $content .= "<RIGHT>实付金额：".$order['paymoney']."</RIGHT>";
                            }else{
                                $content .= "<RIGHT>实付金额：".$order['totalprice']."</RIGHT>";
                            }
                            if($order['formdata']) {
                                foreach ($order['formdata'] as $formdata) {
                                    if($formdata[2] != 'upload') {
                                        if($formdata[0] == '备注') {
                                            $content .= $formdata[0]."：<B>".$formdata[1]."</B><BR>";
                                        } else {
                                            $content .= $formdata[0]."：".$formdata[1]."<BR>";
                                        }
                                    }
                                }
                            }

                            if(getcustom('sys_print_set')){
                                //飞蛾小票底部自定义
                                $boot_custom =  $machine['boot_custom']?$machine['boot_custom']:0;
                                if($boot_custom){
                                    $boot_custom_content = $machine['boot_custom_content'];
                                    if($boot_custom_content){
                                        $content .= "<BR><BR>".$boot_custom_content;
                                    }
                                }
                            }

                            $content .= "<BR><BR>";
                        }
                        $rs = self::feie_print($machine['client_id'],$machine['client_secret'],$machine['machine_code'],$machine['msign'],$content);
                        $printnum++;
                    }

                }elseif($machine['type']==2){//映美云
                    $machinecontent = $machine['tmplcontent'];
                    if(!$machinecontent){
                        return ['status'=>0,'msg'=>'未配置打印模板'];
                    }
                    $machinecontent = json_decode($machinecontent,true);
                    $oglist = [];
                    $ordergoods = db($type.'_order_goods')->where('orderid',$order['id'])->select()->toArray();
                    foreach($ordergoods as  $goods){
                        $oglist[] = [
                            'name'=> $goods['name'],
                            'procode' => $goods['procode'],
                            'num' =>  $goods['num'],
                            'ggname' => $goods['ggname']
                        ];
                    }
                    $textReplaceArr = [
                        '标题'=>$machine['title'],
                        '付款时间'=>date('Y-m-d H:i:s',$order['paytime']),
                        '配送方式'=>$order['freight_text'],
                        '订单号'=>$order['ordernum'],
                        '收货人'=>$order['linkman'],
                        '联系电话'=>$order['tel'],
                        '备注'=>$order['message'],
                    ];
                    $bill_contet =  [];
                    foreach($machinecontent as $key=>$template){
                        $bill_contet[$key]= str_replace(array_keys($textReplaceArr),array_values($textReplaceArr),$template);
                        if($key =='商品信息'){
                            $bill_contet[$key]=['content' => $oglist];
                        }
                        if($key =='bottom'){
                            $bill_contet[$key]=$machine['boot_custom_content'];
                        }
                    }
                    $bill_contet = json_encode($bill_contet,JSON_UNESCAPED_UNICODE);
                    $printerData2['template_id'] = $machine['template_id'];
                    $res = \app\custom\Yingmeiyun::sendPrintTask($machine['client_id'],$machine['client_secret'],$machine['machine_code'],$bill_contet,5,$printerData2);
                    $printnum++;
                }
            }
        }
        if(getcustom('sys_print_set')){
            if(!$order['printdaynum']){
                if($type == 'shop' || $type == 'cashier' || $type == 'maidan' || $type == 'scoreshop'){
                    //更新日单号
                    Db::name($type.'_order')->where('id',$order['id'])->update(['printdaynum'=>$print_day_ordernum]);

                    $print_day_ordernum ++;
                    cache($printdaynum,$print_day_ordernum);
                }
            }
        }
        return ['status'=>1,'msg'=>'成功打印'.$printnum.'张'];
    }

    //打印小票 aid 订单id
    public static function print2($aid,$orderid){
        if(getcustom('form_print')){

            $info = db('form_order')->where('id',$orderid)->where('aid',$aid)->find();
            if(!$info){
                return ['status'=>0,'msg'=>'表单数据不存在'];
            }

            $form = Db::name('form')->where('id',$info['formid'])->where('aid',$aid)->find();

            if(!$form){
                return ['status'=>0,'msg'=>'表单不存在'];
            }
            if(!$form['print_status']){
                return ['status'=>0,'msg'=>'表单打印未开启'];
            }
            if(!$form['printid']){
                return ['status'=>0,'msg'=>'表单未绑定打印机'];
            }
            if(!$form['print_num'] || $form['print_num']<=0){
                return ['status'=>0,'msg'=>'表单打印数量为0'];
            }

            if($form['content']){
                $formcontent = json_decode($form['content'],true);
            }else{
                return ['status'=>0,'msg'=>'表单内容设置为空'];
            }

            if(empty($info['bid'])){
                $info['bid'] = 0;
            }
            if($info['bid'] == 0){
                $binfor = Db::name('admin_set')->where('aid',$aid)->field('id,name,tel')->find();
            }else{
                $binfor = Db::name('business')->where('aid',$aid)->where('id',$info['bid'])->field('id,name,tel')->find();
            }

            $where = [];
            $where[] = ['id', '=', $form['printid']];
            $where[] = ['aid', '=', $aid];
            $machinelist = db('wifiprint_set')->where($where)->where('bid',$info['bid'])->select()->toArray(); //打印机列表
            if(!$machinelist) return ['status'=>0,'msg'=>'没有配置小票打印机'];

            $printnum = 0;
            foreach($machinelist as $machine){
                $num = $form['print_num'];
                for($i=0;$i<$num;$i++){
                    if($machine['type']==0){
                        $content = '';

                        $content .= "<center>".$binfor['name']." </center>\r";
                        
                        $content .= "--------------------------------\r";

                        $title = $info['title']?$info['title']:'';
                        $content .= "表单名称：".$title."\r";

                        $createtime = $info['createtime']?date('Y-m-d H:i:s',$info['createtime']):'';
                        $content .= "提交时间：".$createtime."\r";

                        $content .= "商家电话：".$binfor['tel']."\r";
                        $content .= "--------------------------------\r";

                        if($formcontent){
                            foreach($formcontent as $k=>$field){

                                $pre_title = '';
                                if($field['key']=='separate'){
                                    $pre_title = '<FB>'.$field['val1']."</FB>";
                                }else{
                                    if($field['val3']==1){
                                        $pre_title = '*'.$field['val1']."";
                                    }else{
                                        $pre_title = $field['val1']."";
                                    }
                                }

                                if($field['key'] == 'input' || $field['key'] == 'textarea' || $field['key'] == 'time' || $field['key'] == 'date' || $field['key'] == 'region' || $field['key']== 'upload'){
                                    $content .= $pre_title.'：'.$info['form'.$k]."\r";
                                }else if($field['key'] == 'radio' || $field['key']== 'selector'){
                                    $after = '';
                                    foreach($field['val2'] as $k2=>$v2){
                                        if($info['form'.$k] && $info['form'.$k] == $v2){
                                            $after .= '√'.$v2."\r";
                                        }else{
                                            $after .= $v2."\r";
                                        }
                                    }
                                    unset($v2);
                                    $content .= $pre_title."：\r".$after."\r";
                                }else if($field['key']== 'checkbox'){
                                    $after = '';
                                    foreach($field['val2'] as $k2=>$v2){
                                        if($info['form'.$k] && in_array($v2,explode(',',$info['form'.$k]))){
                                            $after .= '√'.$v2."\r";
                                        }else{
                                            $after .= $v2."\r";
                                        }
                                    }
                                    unset($v2);
                                    $content .= $pre_title."：\r".$after."\r";
                                }

                            }
                            unset($fv);
                        }
                        $content .= "--------------------------------\r";
                        $content .= "打印时间：".date("Y-m-d H:i:s",time())."\r";
                        $rs = self::yilianyun_print($machine['client_id'],$machine['client_secret'],$machine['access_token'],$machine['machine_code'],$machine['msign'],$content);
                        $printnum++;
                    }elseif($machine['type']==1){
                        if($machine['machine_type']==1){ //标签打印机
                            return ['status'=>0,'msg'=>'不支持标签打印机'];
                        }else{
                            $content = '';

                            $content .= "<CB>".$binfor['name']."</CB><BR>";
                            
                            $content .= '--------------------------------<BR>';

                            $title = $info['title']?$info['title']:'';
                            $content .= "表单名称：".$title."<BR>";

                            $createtime = $info['createtime']?date('Y-m-d H:i:s',$info['createtime']):'';
                            $content .= "提交时间：".$createtime."<BR>";

                            $content .= "商家电话：".$binfor['tel']."<BR>";
                            $content .= '--------------------------------<BR>';

                            if($formcontent){
                                foreach($formcontent as $k=>$field){

                                    $pre_title = '';
                                    if($field['key']=='separate'){
                                        $pre_title = '<B>'.$field['val1']."</B>";
                                    }else{
                                        if($field['val3']==1){
                                            $pre_title = '*'.$field['val1']."";
                                        }else{
                                            $pre_title = $field['val1']."";
                                        }
                                    }

                                    if($field['key'] == 'input' || $field['key'] == 'textarea' || $field['key'] == 'time' || $field['key'] == 'date' || $field['key'] == 'region' || $field['key']== 'upload'){
                                        $content .= $pre_title.'：'.$info['form'.$k]."<BR>";
                                    }else if($field['key'] == 'radio' || $field['key']== 'selector'){
                                        $after = '';
                                        foreach($field['val2'] as $k2=>$v2){
                                            if($info['form'.$k] && $info['form'.$k] == $v2){
                                                $after .= '√'.$v2.'<BR>';
                                            }else{
                                                $after .= $v2.'<BR>';
                                            }
                                        }
                                        unset($v2);
                                        $content .= $pre_title.'：<BR>'.$after."<BR>";
                                    }else if($field['key']== 'checkbox'){
                                        $after = '';
                                        foreach($field['val2'] as $k2=>$v2){
                                            if($info['form'.$k] && in_array($v2,explode(',',$info['form'.$k]))){
                                                $after .= '√'.$v2.'<BR>';
                                            }else{
                                                $after .= $v2.'<BR>';
                                            }
                                        }
                                        unset($v2);
                                        $content .= $pre_title.'：<BR>'.$after."<BR>";
                                    }

                                }
                                unset($fv);
                            }
                            $content .= '--------------------------------<BR>';
                            $content .= "打印时间：".date("Y-m-d H:i:s",time())."<BR>";
                        }
                        $rs = self::feie_print($machine['client_id'],$machine['client_secret'],$machine['machine_code'],$machine['msign'],$content);
                        $printnum++;
                    }
                }
            }
            return ['status'=>1,'msg'=>'成功打印'.$printnum.'张'];
        }
    }
    //易联云文本打印 $machine_code:易联云打印机终端号 $msign:易联云终端密钥 $content:打印内容
    public static function yilianyun_print($client_id,$client_secret,$access_token,$machine_code,$msign,$content){
        if(!$machine_code || !$content) return ['status'=>0,'error'=>1,'msg'=>'参数错误'];
        $data = [];
        $data['client_id'] = $client_id;
        $data['access_token'] = $access_token;
        $data['machine_code'] = $machine_code;
        $data['content'] = $content;
        $data['origin_id'] = date('YmdHis').rand(10000,99999);
        $data['timestamp'] = time();
        $data['id'] = self::uuid4();
        $data['sign'] = md5($data['client_id'].$data['timestamp'].$client_secret);
        $rs = request_post('https://open-api.10ss.net/print/index',$data);
        $rs = json_decode($rs,true);
        //dump($rs);
        if($rs['error'] == 8){
            //授权
            $data2 = [];
            $data2['client_id'] = $client_id;
            $data2['machine_code'] = $machine_code;
            $data2['msign'] = $msign;
            $data2['access_token'] = $access_token;
            $data2['timestamp'] = time();
            $data2['id'] = self::uuid4();
            $data2['sign'] = md5($data2['client_id'].$data2['timestamp'].$client_secret);
            $rs2 = request_post('https://open-api.10ss.net/printer/addprinter',$data2);
            //dump($rs2);
            $rs2 = json_decode($rs2,true);
            if($rs2['error'] == 0){
                $rs = request_post('https://open-api.10ss.net/print/index',$data);
                //dump($rs);
                $rs = json_decode($rs,true);
            }else{
                $rs = $rs2;
            }
        }
        //dump($rs);
        if($rs['error']==0){
            $rs['status'] = 1;
            $rs['msg'] = '打印成功';
        }else{
            $rs['status'] = 0;
            $rs['msg'] = $rs['error_description'];
            if($rs['error']==16){
                $rs['msg'] = '终端号配置错误，请检查终端号';
            }
        }
        return $rs;
    }
    //飞鹅小票打印机文本打印
    public static function feie_print($user,$ukey,$sn,$key,$content,$type=0){
        $postdata = [];
        $postdata['user'] = $user;
        $postdata['stime'] = time();
        $postdata['sig'] = sha1($user.$ukey.$postdata['stime']);
        if($type==1){
            $postdata['apiname'] = 'Open_printLabelMsg';
        }else{
            $postdata['apiname'] = 'Open_printMsg';
        }
        $postdata['sn'] = $sn;
        $postdata['content'] = $content;
        $rs = request_post('http://api.feieyun.cn/Api/Open/',$postdata);
        $rs = json_decode($rs,true);
        if($rs['ret'] == 0){
            $rs['status'] = 1;
        }else{
            $rs['status'] = 0;
        }
        return $rs;
    }

    public static function uuid4(){
        mt_srand((double)microtime() * 10000);
        $charid = strtolower(md5(uniqid(rand(), true)));
        $hyphen = '-';
        $uuidV4 =
            substr($charid, 0, 8) . $hyphen .
            substr($charid, 8, 4) . $hyphen .
            substr($charid, 12, 4) . $hyphen .
            substr($charid, 16, 4) . $hyphen .
            substr($charid, 20, 12);
        return $uuidV4;
    }
    //交班打印
    public static function jiaobanPrint($printdata=[]){
        $cashier_info = $printdata['cashier_info'];
        $jiaoban_print_ids =explode(',',$cashier_info['jiaoban_print_ids']);
        $where = [];
        $where[] = ['aid', '=', $cashier_info['aid']];
        $where[] = ['id', 'in', $jiaoban_print_ids];

        $machineList = Db::name('wifiprint_set')->where($where)->select()->toArray();
        if(empty($machineList)) {
            return false;
        }

        foreach ($machineList as $machine) {
            if($machine['type'] ==0){  //易联云
                $content = "交接班对账单\r";
                $content .=  "--------------------------------\r";
                $content .=  "Pos机：1\r";
                $content .=  "收银员：".$printdata['cashdesk_user']."\r";
                $content .=  "开始时间：".$printdata['logintime']."\r";
                $content .=  "结束时间：".$printdata['jiaobantime']."\r";
                $content .=  "———————————————\r";
                $content .=  "<FB>当班营业额，金额 ".$printdata['today_total_money']."</FB> \r";
                if($printdata['cashpay_show']){
                    $content .=  "  -现金           金额：".$printdata['today_cash_money']."\r";
                }
                if($printdata['yuepay_show']){
                    $content .=  "  -".t('会员')."余额       金额：".$printdata['today_yue_money']."\r";
                }
                if($printdata['sxfpay_show']){
                    $content .=  "  -随行付         金额：".$printdata['today_sxf_money']."\r";
                }
                if($printdata['wxpay_show']){
                    $content .=  "  -微信           金额：".$printdata['today_wx_money']."\r";
                }
                if($printdata['alipay_show']) {
                    $content .= "  -支付宝         金额：" . $printdata['today_alipay_money'] . "\r";
                }
                if(false) {
                    $content .=  "  -银行卡         金额：0.00\r";
                    $content .=  "  -抖音团购       金额：0.00\r";
                    $content .=  "  -美团团购\r";
                }
                $content .=  "  -退款明细\r";
                if($printdata['cashpay_show']) {
                    $content .= "      -现金       金额：" . $printdata['today_refund_cash_money'] . "\r";
                }
                if($printdata['sxfpay_show']) {
                    $content .= "      -随行付     金额：" . $printdata['today_refund_sxf_money'] . "\r";
                }
                if($printdata['wxpay_show']) {
                    $content .= "      -微信       金额：" . $printdata['today_refund_wx_money'] . "\r";
                }
                if($printdata['alipay_show']) {
                    $content .= "      -支付宝     金额：" . $printdata['today_refund_alipay_money'] . "\r";
                }
                if(false){
                    $content .=  "      -银行卡     金额：0.00\r";
                    $content .=  "      -抖音团购   金额：0.00\r";
                    $content .=  "      -美团团购\r";

                }
                $content .=  "———————————————\r";
                $content .=  "<FB>会员储值（预付款），金额".$printdata['total_recharge']."</FB> \r";
                if (false){
                    $content .=  "  -现金           金额：0.00\r";
                    $content .=  "  -随行付         金额：0.00\r";
                    $content .=  "  -微信           金额：0.00\r";
                    $content .=  "  -支付宝         金额：0.00\r";
                    $content .=  "  -银行卡         金额：0.00\r";
                    $content .=  "  -退款明细       金额：0.00\r";
                    $content .=  "      -现金       金额：0.00\r";
                    $content .=  "      -随行付     金额：0.00\r";
                    $content .=  "      -微信       金额：0.00\r";
                    $content .=  "      -支付宝     金额：0.00\r";
                    $content .=  "      -银行卡     金额：0.00\r";
                }
                if(false){
                    $content .=  "———————————————\r";
                    $content .=  "<FB>钱箱预留：0.00</FB>\r";
                    $content .=  "<FB>存入备用金：0.00</FB>\r";
                }
                $content .=  "———————————————\r";
                $content .=  "<FB>总收款（实收金额）：".$printdata['total_money']."</FB>\r";
                if(false){
                    $content .=  "<FB>应有现金：0.00</FB>\r";
                    $content .=  "———————————————\r";
                    $content .=  "<FB>上交现金：0.00</FB>\r";
                    $content .=  "<FB>钱箱留存：0.00</FB>\r";
                }
                $content .=  "———————————————\r";
                $content .=  "<FB>打印日期：".date('Y-m-d H:i:s')."</FB>\r";
                $content .= "\r\r";
                $rs = \app\common\Wifiprint::yilianyun_print($machine['client_id'],$machine['client_secret'],$machine['access_token'],$machine['machine_code'],$machine['msign'],$content);
            }elseif ($machine['type']==1){ //飞蛾
                $content = "交接班对账单<BR>";
                $content .=  "--------------------------------<BR>";
                $content .=  "Pos机：1<BR>";
                $content .=  "收银员：".$printdata['cashdesk_user']."<BR>";
                $content .=  "开始时间：".$printdata['logintime']."<BR>";
                $content .=  "结束时间：".$printdata['jiaobantime']."<BR>";
                $content .=  "───────────────<BR>";
                $content .=  "<BOLD>当班营业额，金额 ".$printdata['today_total_money']."</BOLD> <BR>";
                if($printdata['cashpay_show'] ){
                    $content .=  "-现金           金额：".$printdata['today_cash_money']."<BR>";
                }
                if($printdata['yuepay_show']) {
                    $content .= "-" . t('会员') . "余额       金额：" . $printdata['today_yue_money'] . "<BR>";
                }
                if($printdata['sxfpay_show'] ) {
                    $content .= "-随行付         金额：" . $printdata['today_sxf_money'] . "<BR>";
                }
                if($printdata['wxpay_show']) {
                    $content .= "-微信           金额：" . $printdata['today_wx_money'] . "<BR>";
                }
                if($printdata['alipay_show']) {
                    $content .= "-支付宝         金额：" . $printdata['today_alipay_money'] . "<BR>";
                }
                if(false){
                    $content .=  "-银行卡         金额：0.00<BR>";
                    $content .=  "-抖音团购       金额：0.00<BR>";
                    $content .=  "-美团团购<BR>";
                }
                $content .=  "-退款明细<BR>";
                if($printdata['cashpay_show'] > 0) {
                    $content .= "      -现金       金额：" . $printdata['today_refund_cash_money'] . "<BR>";
                }
                if($printdata['sxfpay_show']) {
                    $content .= "      -随行付     金额：" . $printdata['today_refund_sxf_money'] . "<BR>";
                }
                if($printdata['wxpay_show']) {
                    $content .= "      -微信       金额：" . $printdata['today_refund_wx_money'] . "<BR>";
                }
                if($printdata['alipay_show']) {
                    $content .= "      -支付宝     金额：" . $printdata['today_refund_alipay_money'] . "<BR>";
                }
                if(false){
                    $content .=  "      -银行卡     金额：0.00<BR>";
                    $content .=  "      -抖音团购   金额：0.00<BR>";
                    $content .=  "      -美团团购<BR>";

                }
                $content .=  "───────────────<BR>";
                $content .=  "<BOLD>预付款数据(会员储值)，金额 ".$printdata['total_recharge']."</BOLD> <BR>";
                if(false) {
                    $content .= "  -现金           金额：0.00<BR>";
                    $content .= "  -随行付         金额：0.00<BR>";
                    $content .= "  -微信           金额：0.00<BR>";
                    $content .= "  -支付宝         金额：0.00<BR>";
                    $content .= "  -银行卡         金额：0.00<BR>";
                    $content .= "  -退款明细       金额：0.00<BR>";
                    $content .= "      -现金       金额：0.00<BR>";
                    $content .= "      -随行付     金额：0.00<BR>";
                    $content .= "      -微信       金额：0.00<BR>";
                    $content .= "      -支付宝     金额：0.00<BR>";
                    $content .= "      -银行卡     金额：0.00<BR>";
                }
                if(false) {
                    $content .=  "───────────────<BR>";
                    $content .= "<BOLD>钱箱预留：0.00</BOLD><BR>";
                    $content .= "<BOLD>存入备用金：0.00</BOLD><BR>";
                }
                $content .=  "───────────────<BR>";
                $content .=  "<BOLD>总收款（今日实际收到金额）：".$printdata['total_money']."</BOLD><BR>";
                if(false) {
                    $content .=  "<BOLD>应有现金：0.00</BOLD><BR>";
                    $content .=  "<BOLD>上交现金：0.00</BOLD><BR>";
                    $content .=  "<BOLD>钱箱留存：0.00</BOLD><BR>";
                }
                $content .=  "───────────────<BR>";
                $content .=  "<BOLD>打印日期：".date('Y-m-d H:i:s')."</BOLD><BR>";
                $content .= "<BR><BR>";
                $rs = \app\common\Wifiprint::feie_print($machine['client_id'],$machine['client_secret'],$machine['machine_code'],$machine['msign'],$content,$machine['machine_type']);
            }
        }
    }
    
}